package ch10;

import java.io.*;
public class Example10_12 {
    public static void main(String args[]) {
        String command = "渡江总攻时间是4月22日晚10点";
        EncryptAndDecrypt person = new EncryptAndDecrypt();
        String password = "Tiger";
        String secret = person.encrypt(command,password);
        File file=new File("secret.txt");
        try{
            FileOutputStream fos=new FileOutputStream(file);
            DataOutputStream outData=new DataOutputStream(fos);
            outData.writeUTF(secret);
            System.out.println("加密命令:"+secret);
        }
        catch(IOException e){}
        try{
            FileInputStream fis=new FileInputStream(file);
            DataInputStream inData=new DataInputStream(fis);
            String str = inData.readUTF();
            String mingwen = person.decrypt(str,password);
            System.out.println("解密命令:"+mingwen);
        }
        catch(IOException e){}
    }
}

class EncryptAndDecrypt {
    String encrypt(String sourceString,String password) {
        char [] p=password.toCharArray();
        int n = p.length;
        char [] c = sourceString.toCharArray();
        int m = c.length;
        for(int k=0;k<m;k++) {
            int mima = c[k]+p[k%n];
            c[k] = (char)mima;
        }
        return new String(c);
    }
    String decrypt(String sourceString,String password) {
        char [] p=password.toCharArray();
        int n = p.length;
        char [] c = sourceString.toCharArray();
        int m = c.length;
        for(int k=0;k<m;k++) {
            int mima = c[k]-p[k%n];
            c[k] = (char)mima;
        }
        return new String(c);
    }
}